Typical HAL IRDA use cases for migration

This section provides information about typical HAL IRDA use cases for migration.

Important

In HAL2, the IRDA driver is removed, and IRDA functionalities are integrated within the HAL UART driver.

Note

The main difference between HAL1 and HAL2 in transmit and receive functions (polling, interrupt, or DMA) is how the buffer size is specified. HAL1 uses Size to count data elements (u8 or u16), while HAL2 uses Size_byte to specify the size in bytes, providing more precise control.

Use case: IRDA communication in polling mode

This use case shows how to transmit or receive IRDA data in polling mode. In HAL2, IRDA support is handled via the HAL UART driver.

Sequence diagram

    @startuml
    participant "Application" as p1
    participant "HAL IRDA driver" as p2

    == initialization and configuration ==

    == IRDA Processing in polling mode ==
    alt polling
    p1->p2: HAL_IRDA_Transmit(&hirda,\n                                aTxBuffer,\n                                DATA_TO_SEND_SIZE,\n                                100)
    note over p2 #00FF00
    Transmitting data ongoing
    end note

    p2-->p1: <color #Green> HAL_OK
    else
    p1->p2: HAL_IRDA_Receive(&hirda,\n                                aRxBuffer,\n                                DATA_TO_RECEIVE_SIZE,\n                                100)
    note over p2 #00FF00
    Receiving data ongoing
    end note
    p2-->p1: <color #Green> HAL_OK
    end

    @enduml

Code snippet

HAL1

HAL2

#define DATA_TO_SEND_SIZE            256
#define DATA_TO_RECEIVE_SIZE         DATA_TO_SEND_SIZE

uint8_t aTxBuffer[] = " **** IRDA Communication **** ";
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

IRDA_HandleTypeDef hirda;

HAL_IRDA_Init(&hirda);

(void)HAL_IRDA_Receive(&hirda, aRxBuffer, DATA_TO_RECEIVE_SIZE, 100);
(void)HAL_IRDA_Transmit(&hirda, aTxBuffer, DATA_TO_SEND_SIZE, 100);
#define DATA_TO_SEND_SIZE            256
#define DATA_TO_RECEIVE_SIZE         DATA_TO_SEND_SIZE

uint8_t aTxBuffer[] = " **** IRDA Communication **** ";
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

hal_uart_handle_t huart;
hal_uart_irda_config_t uart_irda_config;

HAL_UART_Init(&huart, HAL_UART4);

/* Configure IRDA mode on the UART instance */
HAL_UART_IRDA_SetConfig(&huart, &uart_irda_config);

(void)HAL_UART_Receive(&huart, aRxBuffer, DATA_TO_RECEIVE_SIZE, 100);
(void)HAL_UART_Transmit(&huart, aTxBuffer, DATA_TO_SEND_SIZE, 100);

Key changes

  1. In HAL2, the standalone IRDA driver is removed; use the HAL UART driver with IRDA configuration.

  2. API names are updated from HAL_IRDA_* to HAL_UART_*.


Use case: IRDA receive in interrupt mode

This use case shows how to receive IRDA data in interrupt mode. In HAL2, IRDA support is handled via the HAL UART driver.

Sequence diagram

    @startuml
    participant "Application" as p1
    participant "HAL IRDA driver" as p2
    participant "IRDAX_IRQHandler" as p3

    == initialization and configuration ==
    == IRDA HAL Receive IT ==

    p1->p2: <color: green> HAL_IRDA_Receive_IT(&hirda,  aRxBuffer, DATA_TO_RECEIVE_SIZE)
    note over p2 #00FF00
    Receiving data ongoing
    end note
    p2-->p1: HAL_OK
    p3<-: IRDAX data interrupt
    p3->p2: HAL_IRDA_IRQHandler
    note over p2 #FFFF99
    All data (Size == hirda->RxXferCount) have been received
    end note
    note over p2 #00FF00
    End of receiving data
    end note
    p2->p1: HAL_IRDA_RxCpltCallback(hirda->RxXferCount)

    @enduml

Code snippet

HAL1

HAL2

#define DATA_TO_RECEIVE_SIZE        256

uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

IRDA_HandleTypeDef hirda;

/* Optional: register receive-complete callback */
HAL_IRDA_RegisterCallback(&hirda, HAL_IRDA_RX_CB_ID, IRDA_RxCpltCallback);

/* Start receive in interrupt mode */
(void)HAL_IRDA_Receive_IT(&hirda, aRxBuffer, DATA_TO_RECEIVE_SIZE);
#define DATA_TO_RECEIVE_SIZE        256

uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

hal_uart_handle_t huart;

/* Optional: register receive-complete callback */
HAL_UART_RegisterRxCpltCallback(&huart, UART_RxCpltCallback);

/* Start receive in interrupt mode */
(void)HAL_UART_Receive_IT(&huart, aRxBuffer, DATA_TO_RECEIVE_SIZE);

Key changes

  1. HAL2 uses the UART driver (HAL_UART_Receive_IT() + HAL_UART_IRQHandler()) instead of IRDA-specific APIs.


Use case: IRDA transmit in interrupt mode

This use case shows how to transmit IRDA data in interrupt mode. In HAL2, IRDA support is handled via the HAL UART driver.

Sequence diagram

    @startuml
    participant "Application" as p1
    participant "HAL IRDA driver" as p2
    participant "IRDAX_IRQHandler" as p3

    == initialization and configuration ==
    == IRDA HAL Transmit IT ==

    p1->p2: <color: green> HAL_IRDA_Transmit_IT(&hirda, aTxBuffer, DATA_TO_SEND_SIZE)
    note over p2 #00FF00
    Transmitting data ongoing
    end note
    p2-->p1: HAL_OK
    p3<-: IRDAX data interrupt
    p3->p2: HAL_IRDA_IRQHandler
    note over p2 #FFFF99
    All data (Size == hirda->TxXferCount) have been transmitted
    end note
    note over p2 #00FF00
    End of transmitting data
    end note
    p2->p1: HAL_IRDA_TxCpltCallback(hirda->TxXferCount)

    @enduml

Code snippet

HAL1

HAL2

#define DATA_TO_SEND_SIZE           256

uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** IRDA Communication **** ";

IRDA_HandleTypeDef hirda;

/* Optional: register transmit-complete callback */
HAL_IRDA_RegisterCallback(&hirda, HAL_IRDA_TX_CB_ID, IRDA_TxCpltCallback);

/* Start transmit in interrupt mode */
(void)HAL_IRDA_Transmit_IT(&hirda, aTxBuffer, DATA_TO_SEND_SIZE);
#define DATA_TO_SEND_SIZE           256

uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** IRDA Communication **** ";

hal_uart_handle_t huart;

/* Optional: register transmit-complete callback */
HAL_UART_RegisterTxCpltCallback(&huart, UART_TxCpltCallback);

/* Start transmit in interrupt mode */
(void)HAL_UART_Transmit_IT(&huart, aTxBuffer, DATA_TO_SEND_SIZE);

Key changes

  1. HAL2 uses the UART driver (HAL_UART_Transmit_IT() + HAL_UART_IRQHandler()) instead of IRDA-specific APIs.


Use case: IRDA receive in DMA mode

This use case shows how to receive IRDA data in DMA mode. In HAL2, IRDA support is handled via the HAL UART driver.

Sequence diagram

    @startuml
    participant "Application" as p1
    participant "HAL IRDA driver" as p2
    participant "DMA" as p3
    participant "DMAx_IRQHandler" as p4

    == initialization and configuration ==
    == IRDA HAL Receive DMA ==
    p1->p2: <color: green> HAL_IRDA_Receive_DMA(&hirda, aRxBuffer, DATA_TO_RECEIVE_SIZE)
    note over p2 #00FF00
    Receiving data ongoing
    end note
    p2->p3: HAL_DMA_Start_IT()
    p3-->p2: HAL_OK
    p2-->p1: HAL_OK

    p4<-: DMAx complete interrupt
    p4->p3: HAL_DMA_IRQHandler
    note over p3 #00FF00
    End of receiving data
    end note
    p3->p2: XferCpltCallback
    p2->p1: HAL_IRDA_RxCpltCallback(hirda->rx_xfer_count)
    @enduml

Code snippet

HAL1

HAL2

#define DATA_TO_RECEIVE_SIZE        255

IRDA_HandleTypeDef hirda;
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

/* Optional: register receive-complete callback */
HAL_IRDA_RegisterCallback(&hirda, HAL_IRDA_RX_CB_ID, IRDA_RxCpltCallback);

/* Start receive in DMA mode */
(void)HAL_IRDA_Receive_DMA(&hirda, aRxBuffer, DATA_TO_RECEIVE_SIZE);
#define DATA_TO_RECEIVE_SIZE        255

hal_uart_handle_t huart;
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

/* Optional: register receive-complete callback */
HAL_USART_RegisterRxCpltCallback(&huart, UART_RxCpltCallback);

/* Start receive in DMA mode */
(void)HAL_UART_Receive_DMA(&huart, aRxBuffer, DATA_TO_RECEIVE_SIZE);

Key changes

  1. HAL2 migrates from HAL_IRDA_Receive_DMA() to HAL_UART_Receive_DMA().

  2. HAL2 DMA start helper naming differs (HAL_DMA_StartPeriphXfer_IT_Opt() versus HAL_DMA_Start_IT()).


Use case: IRDA transmit in DMA mode

This use case shows how to transmit IRDA data in DMA mode. In HAL2, IRDA support is handled via the HAL UART driver.

Sequence diagram

    @startuml
    participant "User Application" as p1
    participant "HAL IRDA driver" as p2
    participant "IRDAX_IRQHandler" as p5
    participant "DMA" as p3
    participant "DMAx_IRQHandler" as p4

    == initialization and configuration ==
    == IRDA HAL Transmit DMA ==
    p1->p2: <color: green> HAL_IRDA_Transmit_DMA(&hirda, aTxBuffer, DATA_TO_SEND_SIZE)
    note over p2 #00FF00
    Transmitting data ongoing
    end note
    p2->p3: HAL_DMA_Start_IT()
    p3-->p2: HAL_OK
    p2-->p1: HAL_OK

    p4<-: DMAx complete interrupt
    p4->p3: HAL_DMA_IRQHandler
    note over p3 #00FF00
    End of transmitting data
    end note
    p3->p2: XferCpltCallback
    p2->p2: Enable IRDAx Transmit Complete IRQ (TCIE)
    p5<-: IRDAx Transmit Complete (TCIE)
    p5->p2: HAL_IRDA_IRQHandler
    p2->p1: HAL_IRDA_TxCpltCallback(hirda->TxXferCount)
    @enduml

Code snippet

HAL1

HAL2

#define DATA_TO_SEND_SIZE           255

IRDA_HandleTypeDef hirda;
uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** IRDA Communication **** ";

/* Optional: register transmit-complete callback */
HAL_IRDA_RegisterCallback(&hirda, HAL_IRDA_TX_CB_ID, IRDA_TxCpltCallback);

/* Start transmit in DMA mode */
(void)HAL_IRDA_Transmit_DMA(&hirda, aTxBuffer, DATA_TO_SEND_SIZE);
#define DATA_TO_SEND_SIZE           255

hal_uart_handle_t huart;
uint8_t aTxBuffer[DATA_TO_SEND_SIZE] = " **** IRDA Communication **** ";

/* Optional: register transmit-complete callback */
HAL_UART_RegisterTxCpltCallback(&huart, UART_TxCpltCallback);

/* Start transmit in DMA mode */
(void)HAL_UART_Transmit_DMA(&huart, aTxBuffer, DATA_TO_SEND_SIZE);

Key changes

  1. HAL2 migrates from HAL_IRDA_Transmit_DMA() to HAL_UART_Transmit_DMA().

  2. DMA completion still triggers a TX complete interrupt path, but HAL2 uses HAL_UART_IRQHandler().

  3. Completion callback naming/parameters differ in HAL2 (byte count instead of element count).